import { getHoldDataApi, setHoldDataApi } from "@/api/hold"
import type { SetHoldData, HoldApiData } from "@/api/hold/types"
import { isObject } from "lodash-es"
import { isJSON } from "./is"
import { isArrayString } from "./is"

/**
 * 表单暂存
 * @param data
 * @returns
 */
export async function setHoldData(data: SetHoldData) {
  data.content = isObject(data.content) ? JSON.stringify(data.content) : data.content?.toString()
  return await setHoldDataApi(data)
}

export async function getHoldData<T>(data: HoldApiData): Promise<T> {
  const res = await getHoldDataApi(data)
  if (isArrayString(res)) {
    return JSON.parse(res)
  }
  return isJSON(res)
}